<template>
  <view>
    <view class="goods-list">
      <view v-for="(item, index) in goodsList" :key="index" @click="gotoDetail(item)">
        <my-goods :item="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      queryObj: {
        query: '',
        cid: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      isLoading:false
    };
  },
  onLoad(options) {
    this.queryObj.query = options.query || '';
    this.queryObj.cid = options.cid || '';
    this.getGoodList();
  },
  methods: {
    // 获取商品列表的数据
    async getGoodList(cb) {
      this.isLoading = true  // 开启节流阀
      const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
      this.isLoading = false  // 关闭节流阀
      if (res.meta.status !== 200) nui.$showMsg();
      this.goodsList = [...res.message.goods, ...this.goodsList];
      this.total = res.message.total;
      
      cb && cb()
    },
    gotoDetail(item){
      uni.navigateTo({
        url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
      })
    }
  },
  
  // 上拉加载更多
  onReachBottom() {
    if(this.isLoading || this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return  // 查看节流阀的状态
    // 页码值自增加1
    this.queryObj.pagenum++;
    this.getGoodList();
  },
  
  // 下拉加载更多
  onPullDownRefresh() {
    // 重置数据  +  重新请求
    this.queryObj.pagenum = 1
    this.total = 0
    this.isLoading=false
    this.goodsList = []
    
    this.getGoodList(()=>uni.stopPullDownRefresh())
  }
};
</script>

<style lang="scss"></style>
